﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using com.weijie.tools.Forms;
using FontAwesome.Sharp;

namespace com.weijie.tools
{
    public partial class FormMainMenu : Form
    {
        //Fields
        private IconButton currentBtn;
        // 添加一個选中时候的边框
        private Panel leftBorderBtn;
        private Form currentChildForm;

        // 为了解决鼠标被拖动消耗掉的问题
        private DateTime lastClickTime = DateTime.MinValue;
        private const int doubleClickTime = 500; // 双击间隔时间，单位为毫秒
         
        //Constructor

        public FormMainMenu()
        {
            InitializeComponent();
            leftBorderBtn = new Panel();
            leftBorderBtn.Size = new Size(7, 60);
            panelMenu.Controls.Add(leftBorderBtn);
            //Form  禁用标题栏
            this.Text = string.Empty;
            this.ControlBox = false;
            this.ShowIcon = false;
            this.DoubleBuffered = true;  //激活双缓冲, 以防止拖动的时候窗体闪烁
            // 最大化的时候限制窗口大小为当前窗口工作区的大小, 也就是减去工具栏的高度
            this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
        }

        //Structs
        private struct RGBColors
        {
            public static Color color1 = Color.FromArgb(172, 126, 241);
            public static Color color2 = Color.FromArgb(249, 118, 176);
            public static Color color3 = Color.FromArgb(253, 138, 114);
            public static Color color4 = Color.FromArgb(95, 77, 221);
            public static Color color5 = Color.FromArgb(249, 88, 155);
            public static Color color6 = Color.FromArgb(24, 161, 251);
        }
        //Methods

        // 当点击按钮的时候, 激活按钮时候, 改变按钮颜色
        private void ActivateButton(object senderBtn, Color color)
        {
            if (senderBtn != null)
            {
                 DisableButton();
                //开始定义按钮左边缘要显示的东西
                currentBtn = (IconButton)senderBtn;
                currentBtn.BackColor = Color.FromArgb(37, 36, 81);
                currentBtn.ForeColor = color;
                currentBtn.TextAlign = ContentAlignment.MiddleCenter;
                currentBtn.IconColor = color;
                currentBtn.TextImageRelation = TextImageRelation.TextBeforeImage;
                currentBtn.ImageAlign = ContentAlignment.MiddleRight;
                //Left border button
                leftBorderBtn.BackColor = color;
                leftBorderBtn.Location = new Point(0, currentBtn.Location.Y);
                leftBorderBtn.Size = new Size(leftBorderBtn.Width, currentBtn.Height);
                leftBorderBtn.Visible = true;
                leftBorderBtn.BringToFront();
                //Current Child Form Icon
                iconCurrentChildForm.IconChar = currentBtn.IconChar;
                iconCurrentChildForm.IconColor = color;
            }
        }


        private void DisableButton()
        {
            if (currentBtn != null)
            {
                currentBtn.BackColor = Color.FromArgb(31, 30, 68);
                currentBtn.ForeColor = Color.Gainsboro;
                currentBtn.TextAlign = ContentAlignment.MiddleLeft;
                currentBtn.IconColor = Color.Gainsboro;
                currentBtn.TextImageRelation = TextImageRelation.ImageBeforeText;
                currentBtn.ImageAlign = ContentAlignment.MiddleLeft;
            }
        }


        private void FormMainMenu_Load(object sender, EventArgs e)
        {

        }

        // 每个按钮的点击事件
        private void btnDashboard_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, RGBColors.color1);
           OpenChildForm(new FormDashboard());
        }
        private void btnOrder_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, RGBColors.color2);
           OpenChildForm(new FormOrders());
        }
        private void btnProduct_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, RGBColors.color3);
           OpenChildForm(new FormProducts());
        }
        private void btnCustomer_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, RGBColors.color4);
            OpenChildForm(new FormCustomers());
        }
        private void btnMarketing_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, RGBColors.color5);
           OpenChildForm(new FormMarketing());
        }
        private void btnSetting_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, RGBColors.color6);
            OpenChildForm(new FormSetting());
        }

       

        private void pic_btnHome_Click(object sender, EventArgs e)
        {
            if (currentChildForm != null)
            {
                currentChildForm.Close();
            }
            Reset();
        }


        // child form size = form container Panel size + set the anchor and dock property of the controls appropriately

        private void Reset()
        {
            DisableButton();
            leftBorderBtn.Visible = false;
            iconCurrentChildForm.IconChar = IconChar.Home;
            iconCurrentChildForm.IconColor = Color.MediumPurple;
            lblTitleChildForm.Text = "Home";
        }

 // 拖动窗体
        [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        [DllImport("user32.DLL", EntryPoint = "SendMessage")]
        private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
        private void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (DateTime.Now - lastClickTime < TimeSpan.FromMilliseconds(doubleClickTime))
            {
                // 这是第二次点击，处理双击事件
                panelTitleBar_DoubleClick(sender, e);
            }
            else
            {
                // 这是第一次点击，记录时间
                lastClickTime = DateTime.Now;
                ReleaseCapture();
                SendMessage(this.Handle, 0x112, 0xf012, 0);
            }

            //System.Diagnostics.Debug.WriteLine("响应了按下事件");
            //ReleaseCapture();
            //SendMessage(this.Handle, 0x112, 0xf012, 0);
        }




        private void FormMainMenu_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Maximized)
                FormBorderStyle = FormBorderStyle.None;
            else
                FormBorderStyle = FormBorderStyle.Sizable;
        }



        //打开子窗体
        private void OpenChildForm(Form childForm)
        {
            //只能打开一个窗体
            if (currentChildForm != null)
            {
                currentChildForm.Close();  // 如果想要打开多个窗体, 只需要把这里注释掉就可以
            }
            currentChildForm = childForm;
            //End
            childForm.TopLevel = false;// 不是顶层窗体
            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            panelDesktop.Controls.Add(childForm);
            panelDesktop.Tag = childForm;
            childForm.BringToFront();
            childForm.Show();
            lblTitleChildForm.Text = childForm.Text;
        }


        //Close-Maximize-Minimize
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void btnMaximize_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Normal)
            {
                WindowState = FormWindowState.Maximized;
            }
            else
            {
                WindowState = FormWindowState.Normal;
            }
        }
        private void btnMinimize_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void panelTitleBar_DoubleClick(object sender, EventArgs e)
        {
            //添加双击事件, 如果dag为true, 则双击最大化, 反之最小化
            System.Diagnostics.Debug.WriteLine("响应了双击事件");
            btnMaximize_Click(sender, e);
        }
    }
}
